package com.tong.controller;

import com.tong.pojo.Users;
import com.tong.service.book.BookService;
import com.tong.service.login.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Autowired
    @Qualifier("LoginServiceImpl")
    private LoginService loginService;

    @RequestMapping("/loginPage")
    public String logins() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(Users users,HttpServletResponse request) {
        Users users1 = loginService.singIn(users);
        if(users1 != null){
            // 创建cookie并将成功登陆的用户保存在里面
            Cookie cookie = new Cookie("right", users1.getRights());
            cookie.setMaxAge(60*60*24); // 设置24小时有效
            cookie.setPath("/"); //cookie路径范围
            request.addCookie(cookie);
            return "redirect:/book/allBook";
        }else {
            return "redirect:/loginPage";
        }
    }
}